package org.aurora.six;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author : Aurora
 * @date : 2024/4/15 16:55
 * @description
 * 使用ReentrantReadWriteLock实现线程安全的list
 */
public class ReentrantLockList {
    public List<String> list = new ArrayList<String>();

    public ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    Lock readLock = lock.readLock();
    Lock writeLock = lock.writeLock();

    public void addEle(String ele){
        writeLock.lock();
        try{
            list.add(ele);
        }finally {
            writeLock.unlock();
        }
    }
    public void removeEle(String rEle){
        writeLock.lock();
        try{
            list.remove(rEle);
        }finally {
            writeLock.unlock();
        }
    }

     public String getEle(int i){
        readLock.lock();
        try{
            return list.get(i);
        }finally {
            readLock.unlock();
        }
     }


}
